<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>总示例</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="main" id="app">
    <div class="canvas-box">
      <canvas width="500" height="500" id="c"></canvas>
    </div>
    <div class="opera" id="change">
      <h2>操作区域</h2>
      <p class="tips">ps: 创建一个正方形，请注意参数类型，数字类型：left、top、width、height、angle（ps：支持字符串）；</p>
      <div class="opera-item">
        <label class="opera-item-title" for="">左移{{left}}px：</label>
        <input type="range" step=50 max=500 v-model="left" @change="move('left')">
      </div> 
      <div class="opera-item">
        <label class="opera-item-title" for="">上移{{top}}px：</label>
        <input type="range" step=10 max=500 v-model="top" @change="move('top')">
      </div> 
      <div class="opera-item">
        <div class="opera-item-btn" @click="reset">重置</div>  
      </div> 
    </div>
  </div>
  
</body>
</html>
<script src="bind.js"></script>
<script src="https://unpkg.com/vue@next"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.4.0/fabric.min.js"></script>
<script>
const vm = Vue.createApp({
  data() {
    return {
      canvas:null,
      left: 250,
      top:250,
      commonConfig:{
        left: 240, 
        top: 240,
        width: 50,
        height: 50,
        angle: 45
      },
      rect:null,
      rectConfig:{
        fill: 'red',
      },
      circle:null,
      circleConfig:{
        fill:'#f66'
      },
      triangle:null,
      triangleConfig:{
        fill:'pink'
      }
    }
  },
  mounted(){
    // 画布初始化s
    this.canvas = new fabric.Canvas('c');
    const canvas = this.canvas;
    // 水平及其垂直缩放锁定，中心点确定
    canvas.lockScalingX = canvas.lockScalingY = true;
    this.rect = new fabric.Rect({...this.commonConfig,...this.rectConfig});
    canvas.add(this.rect);
  },
  methods:{
    move(type){
      const config = {};
      config[type]=Number(this[type])
      this.canvas.item(0).set(config)
      this.canvas.renderAll();
    },
    reset(){
      this.canvas.item(0).set(this.commonConfig);
      this.canvas.renderAll();
    }
  }
}).mount('#app')

  

</script>